package cn.iocoder.yudao.module.system.controller.admin.gugu;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.*;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

/**
 * JSON 序列化测试控制器
 *
 * @author 芋道源码
 */
@Tag(name = "管理后台 - JSON测试")
@RestController
@RequestMapping("/system/json-test")
@Validated
@Slf4j
public class JsonTestController {

    @GetMapping("/test-json")
    @Operation(summary = "测试JSON序列化")
    public CommonResult<Map<String, Object>> testJson() {
        log.info("开始测试JSON序列化");
        
        try {
            Map<String, Object> result = new HashMap<>();
            
            // 测试基本数据类型
            List<String> tags = Arrays.asList("985", "211", "双一流");
            String tagsJson = JsonUtils.toJsonString(tags);
            result.put("tags", tags);
            result.put("tagsJson", tagsJson);
            
            // 测试历史分数数据格式
            List<Map<String, String>> historyScore = new ArrayList<>();
            Map<String, String> score2024 = new HashMap<>();
            score2024.put("2024", "595,12000,45");
            historyScore.add(score2024);
            
            Map<String, String> score2023 = new HashMap<>();
            score2023.put("2023", "588,13500,42");
            historyScore.add(score2023);
            
            String historyScoreJson = JsonUtils.toJsonString(historyScore);
            result.put("historyScore", historyScore);
            result.put("historyScoreJson", historyScoreJson);
            
            // 测试等位分数据格式
            List<Map<String, String>> parityScore = new ArrayList<>();
            Map<String, String> parity2024 = new HashMap<>();
            parity2024.put("2024", "595");
            parityScore.add(parity2024);
            
            Map<String, String> parity2023 = new HashMap<>();
            parity2023.put("2023", "592");
            parityScore.add(parity2023);
            
            String parityScoreJson = JsonUtils.toJsonString(parityScore);
            result.put("parityScore", parityScore);
            result.put("parityScoreJson", parityScoreJson);
            
            // 测试招生计划数据格式
            List<Map<String, Integer>> planData = new ArrayList<>();
            Map<String, Integer> plan2024 = new HashMap<>();
            plan2024.put("2024", 45);
            planData.add(plan2024);
            
            Map<String, Integer> plan2023 = new HashMap<>();
            plan2023.put("2023", 42);
            planData.add(plan2023);
            
            String planDataJson = JsonUtils.toJsonString(planData);
            result.put("planData", planData);
            result.put("planDataJson", planDataJson);
            
            result.put("message", "JSON序列化测试成功");
            result.put("timestamp", new Date());
            
            return success(result);
        } catch (Exception e) {
            log.error("JSON序列化测试失败", e);
            return CommonResult.error(500, "JSON序列化测试失败：" + e.getMessage());
        }
    }

    @GetMapping("/test-parse")
    @Operation(summary = "测试JSON反序列化")
    public CommonResult<Map<String, Object>> testParse() {
        log.info("开始测试JSON反序列化");
        
        try {
            Map<String, Object> result = new HashMap<>();
            
            // 测试解析标签JSON
            String tagsJson = "[\"985\",\"211\",\"双一流\"]";
            List<String> tags = JsonUtils.parseArray(tagsJson, String.class);
            result.put("originalTagsJson", tagsJson);
            result.put("parsedTags", tags);
            
            // 测试解析历史分数JSON
            String historyScoreJson = "[{\"2024\":\"595,12000,45\"},{\"2023\":\"588,13500,42\"}]";
            List<Map> historyScore = JsonUtils.parseArray(historyScoreJson, Map.class);
            result.put("originalHistoryScoreJson", historyScoreJson);
            result.put("parsedHistoryScore", historyScore);
            
            result.put("message", "JSON反序列化测试成功");
            result.put("timestamp", new Date());
            
            return success(result);
        } catch (Exception e) {
            log.error("JSON反序列化测试失败", e);
            return CommonResult.error(500, "JSON反序列化测试失败：" + e.getMessage());
        }
    }

    @GetMapping("/health")
    @Operation(summary = "健康检查")
    public CommonResult<String> health() {
        return success("JSON测试接口运行正常，JsonUtils可用");
    }
}
